<template>
  <div id="zybm-container">
    <div v-if="isShow">
      <div class="top">
        <div class="addBtn">
          <el-button size="mini" type="primary" @click="addHandle()">新增目录</el-button>
        </div>
        <div class="search">
          <el-select
            ref="select1"
            v-model="ztVal"
            style="margin-right: 10px"
            size="mini"
            placeholder="编目状态"
            clearable
          >
            <el-option v-for="item in ztOptions" :key="item.label" :label="item.label" :value="item.value" />
          </el-select>
          <el-select
            ref="select2"
            v-model="gxVal"
            style="margin-right: 10px"
            size="mini"
            placeholder="共享类型"
            clearable
          >
            <el-option v-for="item in gxOptions" :key="item.label" :label="item.label" :value="item.value" />
          </el-select>
          <el-input v-model="searchInp" size="mini" placeholder="请输入编目名称" clearable />
          <el-button size="mini" type="primary" @click="initData()">查 询</el-button>
          <!-- <el-button size="mini" plain>重 置</el-button> -->
        </div>
      </div>
      <div class="dataTable">
        <el-table
          ref="multipleTable"
          :data="dataList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="catalogName" label="编目名称" show-overflow-tooltip />
          <el-table-column label="信息资源提供方">
            <template slot-scope="scope">
              <!-- 先判断dataSource是否存在，再访问name属性 -->
              {{ (scope.row.dataSource && scope.row.dataSource.name) || '-' }}
            </template>
          </el-table-column>
          <el-table-column prop="catalogDescription" label="信息资源摘要" show-overflow-tooltip />
          <el-table-column prop="updateCycle" label="更新周期" show-overflow-tooltip>
            <template #default="scope">
              {{ formatUpdateCycle(scope.row.updateCycle) }}
            </template>
          </el-table-column>
          <el-table-column prop="dataClassification" label="数据分级" show-overflow-tooltip>
            <template #default="scope">
              {{
                scope.row.dataClassification === 0 ? '一般数据' :
                scope.row.dataClassification === 1 ? '重要数据' :
                scope.row.dataClassification === 2 ? '核心数据' :
                '未知分级'
              }}
            </template>
          </el-table-column>
          <el-table-column prop="dataVolume" label="数据量" show-overflow-tooltip />
          <el-table-column prop="dataBeginTime" label="数据开始时间" show-overflow-tooltip />
          <el-table-column prop="dataEndTime" label="数据结束时间" show-overflow-tooltip />
          <el-table-column prop="governmentDataProvider" label="政务数据提供方" show-overflow-tooltip />
          <el-table-column prop="sharedType" label="共享类型" show-overflow-tooltip>
            <template #default="scope">
              {{
                scope.row.sharedType === 0 ? '无条件共享' :
                scope.row.sharedType === 1 ? '有条件共享' :
                scope.row.sharedType === 2 ? '不子共享' :
                '未知共享条件'
              }}
            </template>
          </el-table-column>
          <el-table-column prop="sharedCondition" label="共享条件" show-overflow-tooltip />
          <el-table-column prop="isPublic" label="是否向社会开放" show-overflow-tooltip>
            <template #default="scope">
              {{
                scope.row.isPublic === 0 ? '是' :
                scope.row.isPublic === 1 ? '否' : '未知'
              }}
            </template>
          </el-table-column>
          <el-table-column prop="catalogStatus" label="数据编目状态" show-overflow-tooltip>
            <template #default="scope">
              {{ formatCatalogStatus(scope.row.catalogStatus) }}
            </template>
          </el-table-column>
          <el-table-column prop="sharedWay" label="共享方式" show-overflow-tooltip>
            <template #default="scope">
              {{ (scope.row.sharedWay || []).map(item => item.sharedName).join(', ') }}
            </template>
          </el-table-column>
          <el-table-column label="信息资源分类" show-overflow-tooltip>
            <template slot-scope="scope">
              <!-- 先判断数组是否存在且有数据，再处理 -->
              <template v-if="scope.row.dataNodes && scope.row.dataNodes.length > 0">
                {{ scope.row.dataNodes.map(item => item.name).join('，') }}
              </template>
              <template v-else>-</template>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="280">
            <template slot-scope="scope">
              <!-- 查看按钮：始终启用 -->
              <el-button
                type="text"
                size="small"
                @click="infoHandle(scope.row)"
              >
                查看
              </el-button>

              <!-- 修改按钮：仅在状态0、3、5时启用 -->
              <el-button
                type="text"
                size="small"
                :disabled="![0, 3, 5].includes(scope.row.catalogStatus)"
                @click="updatecataFn(scope.row)"
              >
                修改
              </el-button>

              <!-- 删除按钮：仅在状态0、3、5时启用 -->
              <el-button
                type="text"
                size="small"
                :disabled="![0, 3, 5].includes(scope.row.catalogStatus)"
                @click="delHandle(scope.row)"
              >
                删除
              </el-button>

              <!-- 上线按钮：仅在状态0、3、5时启用 -->
              <el-button
                type="text"
                size="small"
                :disabled="![0, 3, 5].includes(scope.row.catalogStatus)"
                @click="onlineHandle(scope.row)"
              >
                上线
              </el-button>

              <!-- 下线按钮：仅在状态2、6时启用 -->
              <el-button
                type="text"
                size="small"
                :disabled="![2, 6].includes(scope.row.catalogStatus)"
                @click="offlineHandle(scope.row)"
              >
                下线
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <Pagination :total-num="totalNum" :current-page="currentPage" @handleCurrentChange="handleCurrentChange" @handlePageSizeChange="handlePageSizeChange" />
    </div>
    <zybm-info v-if="!isShow" ref="zybmInfo" @isShowHandle="isShowHandle" @handleinit="initData" />
  </div>
</template>

<script>
import Pagination from '@/components/common/Pagination.vue'
import { getSelectcatalog, getDelcatalog, addApplication, getCatalogdetail } from '@/api/zybm'
import zybmInfo from './zybmInfo.vue'

export default {
  name: 'SjzyZybm',
  components: {
    Pagination,
    zybmInfo
  },
  data() {
    return {
      searchInp: '',
      isShow: true,
      totalNum: 0,
      currentPage: 1,
      pageSize: 10,
      ztVal: null,
      ztOptions: [
        {
          value: 0,
          label: '待发布'
        },
        {
          value: '1',
          label: '发布待审核'
        },
        {
          value: '2',
          label: '已发布'
        },
        {
          value: '3',
          label: '发布驳回'
        },
        {
          value: '4',
          label: '下线待审核'
        },
        {
          value: '5',
          label: '已下线'
        },
        {
          value: '6',
          label: '下线驳回'
        }
      ],
      gxVal: null,
      gxOptions: [
        {
          value: '0',
          label: '无条件共享'
        },
        {
          value: '1',
          label: '有条件共享'
        },
        {
          value: '2',
          label: '不予共享'
        }
      ],
      dataList: []
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    // 上线操作处理
    onlineHandle(row) {
      this.$confirm('确定上线该条数据吗？', '提示', { type: 'warning' })
        .then(() => {
          // 处理上线逻辑
          addApplication({
            catalogName: row.catalogName,
            catalogId: row.id,
            applicationType: 0
          }).then(res => {
            this.$message({
              message: res,
              type: 'success'
            })
            this.initData()
          })
            .catch(() => this.$message.error('上线失败'))
        })
        .catch(() => this.$message.info('已取消上线'))
    },

    // 下线操作处理
    offlineHandle(row) {
      this.$confirm('确定下线该条数据吗？', '提示', { type: 'warning' })
        .then(() => {
          // 处理下线逻辑
          addApplication({
            catalogName: row.catalogName,
            catalogId: row.id,
            applicationType: 1
          }).then(res => {
            this.$message({
              message: res,
              type: 'success'
            })
            this.initData()
          })
            .catch(() => this.$message.error('下线失败'))
        })
        .catch(() => this.$message.info('已取消下载'))
    },
    infoHandle(row) {
      getCatalogdetail(row.id).then(res => {
        this.isShow = false
        this.$nextTick(() => {
          // 传递查看模式标记和数据
          this.$refs.zybmInfo.setViewData(res)
        })
      })
    },
    formatUpdateCycle(val) {
      const map = { 0: '实时', 1: '每天', 2: '每周', 3: '每月', 4: '每季度', 5: '每半年', 6: '每年', 7: '其他' }
      return map[val] || val
    },
    formatCatalogStatus(val) {
      const map = {
        0: '待发布',
        1: '发布待审核',
        2: '已发布',
        3: '发布驳回',
        4: '下线待审核',
        5: '已下线',
        6: '下线驳回'
      }
      return map[val] || val
    },
    delHandle(row) {
      this.$confirm('确定删除该数据项吗？', '提示', { type: 'warning' })
        .then(() => {
          // 编辑状态：调用后端删除接口（假设接口为deleteCatalogItem）
          getDelcatalog(row.id)
            .then(() => {
              this.$message.success('删除成功')
              this.initData() // 刷新当前页
            })
            .catch(() => this.$message.error('删除失败'))
        })
        .catch(() => this.$message.info('已取消删除'))
    },
    handleSelectionChange() {
    },
    initData() {
      const params = {
        catalogStatus: this.ztVal,
        catalogName: this.searchInp,
        sharedType: this.gxVal,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }
      getSelectcatalog(params).then(res => {
        console.log(res, '列表数据')
        this.dataList = res.content
        this.totalNum = res.totalElements
      })
    },
    isShowHandle() {
      this.isShow = true
      this.initData() // 关键：返回列表时重新加载数据
    },
    addHandle() {
      this.isShow = false
      this.$nextTick(() => {
        this.$refs.zybmInfo.jbxxFlag = true
        // 新增时重置表单
        this.$refs.zybmInfo.resetForm()
      })
    },
    updatecataFn(row) {
      console.log(row, '修改')
      this.isShow = false
      this.$nextTick(() => {
        this.$refs.zybmInfo.setEditData(row)
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.initData()
    },
    handlePageSizeChange(val) {
      console.log(val, 'val')
      this.pageSize = val
      this.currentPage = 1
      this.initData()
    }
  }
}
</script>

<style scoped src="@/assets/css/zybm.css"></style>
